.EXPORT_ALL_VARIABLES:

.PHONY: clean all

ROOTCFLAGS   := $(shell root-config --cflags)
ROOTCOMMONS     := $(shell root-config --libs)
ROOTGCOMMONS    := $(shell root-config --glibs)
ROOTINC      := -I$(shell root-config --incdir)

COMMON_DIR = ../../../Common

MED_DIR = ./Med

ALCOMMONS  = -lm $(ROOTCOMMONS) $(ROOTGCOMMONS) -L$(COMMON_DIR) -lCommandLineInterface

CC		= gcc
CPP             = c++
CFLAGS		= -O2 -pipe -Wall -W -Woverloaded-virtual $(ROOTCFLAGS)

#DllSuf		= dylib

INCLUDES        = -I$(MED_DIR)/inc -I$(COMMON_DIR) -I.
LFLAGS		= -O2 -g
#SOFLAGS 		= -dynamiclib -single_module -undefined dynamic_lookup

COMMONS 		= $(ALCOMMONS)

O_FILES = BuiltEvent.o \
	Datas.o \
	EventBuffer.o \
	EventBuilder.o \
	GlobalSettings.o \
	Modules.o \
	SubEvents.o \
	UnpackedEvent.o \
	$(MED_DIR)/mbsio.o \
	$(MED_DIR)/byte_order.o \
	TransferEventBuilderDictionary.o

all: TransferEventBuilder BuiltEventHistograms
	echo Done

TransferEventBuilder: TransferEventBuilder.cc $(O_FILES) $(COMMON_DIR)/libCommandLineInterface.a
	$(CPP) $(CFLAGS) $(INCLUDES) TransferEventBuilder.cc $(O_FILES) $(COMMONS) -o $@
	$(AR) cru $(COMMON_DIR)/libTransferEventBuilder.a $(O_FILES)
#	cp TransferEventBuilder $(HOME)/bin

$(MED_DIR)/mbsio.o: $(MED_DIR)/src/mbsio.c $(MED_DIR)/inc/mbsio.h
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(MED_DIR)/byte_order.o: $(MED_DIR)/src/byte_order.c $(MED_DIR)/inc/byte_order.h 
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(COMMON_DIR)/libCommandLineInterface.a:
	make -C $(COMMON_DIR)

%.o: %.cc %.hh
	$(CPP) $(CFLAGS) $(INCLUDES) -c $< -o $@

BuiltEventHistograms: BuiltEventHistograms.cc
	$(CPP) $(CFLAGS) $(INCLUDES) BuiltEventHistograms.cc $(O_FILES) $(COMMONS) -o $@
#	cp BuiltEventHistograms $(HOME)/bin

ScalerHistograms: ScalerHistograms.cc
	$(CPP) $(CFLAGS) $(INCLUDES) ScalerHistograms.cc $(O_FILES) $(COMMONS) -o $@

#---- Root stuff

DEPENDENCIES = BuiltEvent.hh Datas.hh EventBuffer.hh EventBuilder.hh GlobalSettings.hh Modules.hh SubEvents.hh UnpackedEvent.hh RootLinkDef.h 

TransferEventBuilderDictionary.o: TransferEventBuilderDictionary.cc TransferEventBuilderDictionary.h
	 $(CPP) -fPIC $(CFLAGS) $(INCLUDES) -c $<

TransferEventBuilderDictionary.cc: $(DEPENDENCIES)
	 rm -f TransferEventBuilderDictionary.cc TransferEventBuilderDictionary.h; rootcint -f $@ -c $(INCLUDES) $(DEPENDENCIES)

#--------------------------------------------------

tar:
	@echo "creating zipped tar-ball ... "
	tar -chvzf mbsio.tar.gz Makefile RootLinkDef.h \
	*.hh *.cc \
	ReadMe.txt Is454Settings*.dat

#--------------------------------------------------
clean:
	rm  -f TransferEventBuilder *.o TransferEventBuilderDictionary.cc TransferEventBuilderDictionary.h Med/*.o $(COMMON_DIR)/libTransferEventBuilder.a BuiltEventHistograms
	make -C $(COMMON_DIR) clean
